package com.mlz.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mlz.mapper.EmpMapper;
import com.mlz.pojo.Emp;
import com.mlz.pojo.PageBean;
import com.mlz.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;


   /* @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1.获取总记录数
        Long count = empMapper.count();
        //2.获取分页查询结果列表
        //计算起始索引: (当前页面-1)*页大小
        Integer start = (page - 1) * pageSize;
        List<Emp> empList = empMapper.list(start,pageSize);

        //3.封装pageBean对象
        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
    }*/


    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page, pageSize);
        //2.执行查询
        List<Emp> list = empMapper.list(name,gender,begin,end);
        //System.out.println(list);
        Page<Emp> p = (Page<Emp>) list;
        //3.封装pageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }



    @Override
    public void save(Emp emp) {
        //补全数据
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //调用添加方法
        empMapper.insert(emp);
    }

    @Override
    public Emp getById(Integer id) {
        /*Emp emp = empMapper.findById(id);
        return emp;*/
        return empMapper.findById(id);
    }

    @Override
    public void update(Emp emp) {
        //前端请求参数接过来 + 手动封装信息（实时的时间）
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        //调用mapper层的功能,进行查询
        Emp loginEmp = empMapper.getUsernameAndPassword(emp);
        return loginEmp;
    }


}
